/**
 * @file ai_glass_callback_example.c
 * @brief AI Glass回调框架使用示例
 * @author [Your Name]
 * @date 2025-09-28
 * @version 1.0
 * @copyright Copyright (c) 2025
 */

#include "ai_glass_callback.h"
#include "ai_glass_log.h"
#include "ai_glass_common.h"

/* ==================== 回调函数示例 ==================== */

/**
 * @brief 拍照模块回调函数
 */
static void photo_callback(const ai_glass_callback_info_t* info, void* user_data)
{
    ai_glass_log_info("拍照回调: 任务ID=%u, 状态=%s, 进度=%u%%, 消息=%s",
                     info->task_id, ai_glass_get_state_string(info->state),
                     info->progress, info->message);
    
    switch (info->state) {
        case AI_GLASS_STATE_RUNNING:
            ai_glass_log_info("拍照进行中...");
            break;
        case AI_GLASS_STATE_COMPLETED:
            if (info->error_code == AI_GLASS_ERR_SUCCESS) {
                ai_glass_log_info("拍照成功完成");
                // 通知RTT端拍照成功
                set_glass_info_v4_notify(glass_fd, CMD_PHOTO, (char*)&info->error_code, sizeof(int));
            } else {
                ai_glass_log_error("拍照失败: %s", ai_glass_get_error_string(info->error_code));
                // 通知RTT端拍照失败
                int result = 0;
                set_glass_info_v4_notify(glass_fd, CMD_PHOTO, (char*)&result, sizeof(int));
            }
            break;
        case AI_GLASS_STATE_ERROR:
            ai_glass_log_error("拍照出现错误: %s", ai_glass_get_error_string(info->error_code));
            break;
        default:
            break;
    }
}

/**
 * @brief 录像模块回调函数
 */
static void record_callback(const ai_glass_callback_info_t* info, void* user_data)
{
    static char* record_file_name = (char*)user_data;
    
    ai_glass_log_info("录像回调: 任务ID=%u, 状态=%s, 进度=%u%%, 消息=%s",
                     info->task_id, ai_glass_get_state_string(info->state),
                     info->progress, info->message);
    
    switch (info->state) {
        case AI_GLASS_STATE_INITIALIZING:
            ai_glass_log_info("录像初始化中...");
            break;
        case AI_GLASS_STATE_RUNNING:
            ai_glass_log_info("录像进行中... 进度: %u%%", info->progress);
            break;
        case AI_GLASS_STATE_STOPPING:
            ai_glass_log_info("录像停止中...");
            break;
        case AI_GLASS_STATE_COMPLETED:
            if (info->error_code == AI_GLASS_ERR_SUCCESS) {
                ai_glass_log_info("录像成功完成, 文件: %s", record_file_name ? record_file_name : "未知");
                // 通知RTT端录像成功
                int result = 1;
                set_glass_info_v4_notify(glass_fd, CMD_RECORD, (char*)&result, sizeof(int));
            } else if (info->error_code == AI_GLASS_ERR_RECORD_TIME_TOO_SHORT) {
                ai_glass_log_warn("录像时间过短，文件已删除");
                // 通知RTT端录像失败
                int result = -1;
                set_glass_info_v4_notify(glass_fd, CMD_RECORD, (char*)&result, sizeof(int));
            }
            break;
        case AI_GLASS_STATE_ERROR:
            ai_glass_log_error("录像出现错误: %s", ai_glass_get_error_string(info->error_code));
            break;
        default:
            break;
    }
}

/**
 * @brief 音频录制回调函数
 */
static void audio_callback(const ai_glass_callback_info_t* info, void* user_data)
{
    ai_glass_log_info("音频回调: 任务ID=%u, 状态=%s, 进度=%u%%, 消息=%s",
                     info->task_id, ai_glass_get_state_string(info->state),
                     info->progress, info->message);
    
    switch (info->state) {
        case AI_GLASS_STATE_COMPLETED:
            if (info->error_code == AI_GLASS_ERR_SUCCESS) {
                int result = 1;
                set_glass_info_v4_notify(glass_fd, CMD_AUDIO_RECORD, (char*)&result, sizeof(int));
            } else {
                int result = -1;
                set_glass_info_v4_notify(glass_fd, CMD_AUDIO_RECORD, (char*)&result, sizeof(int));
            }
            break;
        case AI_GLASS_STATE_ERROR:
            ai_glass_log_error("音频录制错误: %s", ai_glass_get_error_string(info->error_code));
            break;
        default:
            break;
    }
}

/**
 * @brief RTSP模块回调函数
 */
static void rtsp_callback(const ai_glass_callback_info_t* info, void* user_data)
{
    ai_glass_log_info("RTSP回调: 任务ID=%u, 状态=%s, 消息=%s",
                     info->task_id, ai_glass_get_state_string(info->state), info->message);
    
    switch (info->state) {
        case AI_GLASS_STATE_RUNNING:
            ai_glass_log_info("RTSP流已启动");
            // 通知RTT端RTSP启动成功
            int result = 1;
            set_glass_info_v4_notify(glass_fd, CMD_LIVE_STREAM, (char*)&result, sizeof(int));
            break;
        case AI_GLASS_STATE_COMPLETED:
            ai_glass_log_info("RTSP流已停止");
            break;
        case AI_GLASS_STATE_ERROR:
            ai_glass_log_error("RTSP错误: %s", ai_glass_get_error_string(info->error_code));
            break;
        default:
            break;
    }
}

/**
 * @brief 系统通用回调函数
 */
static void system_callback(const ai_glass_callback_info_t* info, void* user_data)
{
    ai_glass_log_info("系统回调: 模块=%s, 任务ID=%u, 状态=%s, 错误码=0x%x, 消息=%s",
                     ai_glass_get_module_string(info->module), info->task_id,
                     ai_glass_get_state_string(info->state), info->error_code, info->message);
}

/* ==================== 集成示例函数 ==================== */

/**
 * @brief 初始化回调系统
 * @return 0成功，其他值失败
 */
int ai_glass_init_callback_system(void)
{
    // 初始化回调框架
    if (ai_glass_callback_init() != 0) {
        ai_glass_log_error("回调框架初始化失败");
        return -1;
    }
    
    // 注册各模块回调函数
    
    // 注册拍照回调(关注所有状态)
    if (!ai_glass_callback_register(AI_GLASS_MODULE_CAMERA, 0, 0, photo_callback, NULL)) {
        ai_glass_log_error("注册拍照回调失败");
        return -1;
    }
    
    // 注册录像回调(关注所有状态)
    if (!ai_glass_callback_register(AI_GLASS_MODULE_RECORD, 0, 0, record_callback, NULL)) {
        ai_glass_log_error("注册录像回调失败");
        return -1;
    }
    
    // 注册音频回调(只关注完成和错误状态)
    uint32_t audio_state_mask = (1 << AI_GLASS_STATE_COMPLETED) | (1 << AI_GLASS_STATE_ERROR);
    if (!ai_glass_callback_register(AI_GLASS_MODULE_AUDIO, 0, audio_state_mask, audio_callback, NULL)) {
        ai_glass_log_error("注册音频回调失败");
        return -1;
    }
    
    // 注册RTSP回调
    if (!ai_glass_callback_register(AI_GLASS_MODULE_RTSP, 0, 0, rtsp_callback, NULL)) {
        ai_glass_log_error("注册RTSP回调失败");
        return -1;
    }
    
    // 注册系统通用回调(监控所有模块的错误状态)
    uint32_t error_state_mask = (1 << AI_GLASS_STATE_ERROR);
    if (!ai_glass_callback_register(AI_GLASS_MODULE_SYSTEM, 0, error_state_mask, system_callback, NULL)) {
        ai_glass_log_error("注册系统回调失败");
        return -1;
    }
    
    ai_glass_log_info("回调系统初始化完成");
    return 0;
}

/* ==================== 修改后的拍照函数示例 ==================== */

/**
 * @brief 带回调通知的拍照函数
 * @param isp_conf_path ISP配置路径
 * @param fps 帧率
 * @param frame_depth 帧深度
 * @return 任务ID，0表示失败
 */
uint32_t run_take_photo_with_callback(char *isp_conf_path, int fps, int frame_depth)
{
    // 生成任务ID
    uint32_t task_id = ai_glass_callback_generate_task_id();
    
    // 通知开始拍照
    AI_GLASS_NOTIFY_STATE(task_id, AI_GLASS_MODULE_CAMERA, CMD_PHOTO, 
                         AI_GLASS_STATE_RUNNING, "开始拍照");
    
    // 执行拍照
    int ret = ak_snap_env_init(isp_conf_path, fps, frame_depth);
    if (ret != 0) {
        AI_GLASS_NOTIFY_ERROR(task_id, AI_GLASS_MODULE_CAMERA, CMD_PHOTO,
                             AI_GLASS_ERR_CAMERA_INIT_FAILED, "相机初始化失败");
        return 0;
    }
    
    // 通知拍照进行中
    AI_GLASS_NOTIFY_PROGRESS(task_id, AI_GLASS_MODULE_CAMERA, CMD_PHOTO, 50, "拍照进行中");
    
    ret = ak_snap_photo(PICTURES_PATH);
    if (ret != 0) {
        AI_GLASS_NOTIFY_ERROR(task_id, AI_GLASS_MODULE_CAMERA, CMD_PHOTO,
                             AI_GLASS_ERR_CAPTURE_FAILED, "拍照失败");
        ak_snap_env_destory();
        return 0;
    }
    
    ret = ak_snap_env_destory();
    if (ret != 0) {
        ai_glass_log_warn("相机资源释放失败");
    }
    
    // 通知拍照完成
    AI_GLASS_NOTIFY_SUCCESS(task_id, AI_GLASS_MODULE_CAMERA, CMD_PHOTO, 100, "拍照完成");
    
    return task_id;
}

/* ==================== 修改后的录像函数示例 ==================== */

/**
 * @brief 带回调通知的录像函数
 * @param isp_conf_path ISP配置路径
 * @param record_direction 录像方向
 * @param record_sec 录像时长
 * @param record_min_sec 最小录像时长
 * @param frame_depth 帧深度
 * @param record_file_name_out 输出文件名
 * @return 任务ID，0表示失败
 */
uint32_t run_record_with_callback(char *isp_conf_path, int record_direction,
                                 int record_sec, int record_min_sec, int frame_depth,
                                 char **record_file_name_out)
{
    uint32_t task_id = ai_glass_callback_generate_task_id();
    
    // 通知录像初始化
    AI_GLASS_NOTIFY_STATE(task_id, AI_GLASS_MODULE_RECORD, CMD_RECORD,
                         AI_GLASS_STATE_INITIALIZING, "录像初始化中");
    
    // 执行录像初始化
    int ret = ak_record_env_init(isp_conf_path, frame_depth);
    if (ret != 0) {
        AI_GLASS_NOTIFY_ERROR(task_id, AI_GLASS_MODULE_RECORD, CMD_RECORD,
                             AI_GLASS_ERR_RECORD_INIT_FAILED, "录像初始化失败");
        return 0;
    }
    
    // 通知录像开始
    AI_GLASS_NOTIFY_STATE(task_id, AI_GLASS_MODULE_RECORD, CMD_RECORD,
                         AI_GLASS_STATE_RUNNING, "录像开始");
    
    // 模拟录像进度通知
    for (int i = 0; i < record_sec; i++) {
        if (get_record_thread_exit()) {
            break;
        }
        
        uint32_t progress = (i * 100) / record_sec;
        char progress_msg[64];
        snprintf(progress_msg, sizeof(progress_msg), "录像进行中 %d/%d秒", i+1, record_sec);
        AI_GLASS_NOTIFY_PROGRESS(task_id, AI_GLASS_MODULE_RECORD, CMD_RECORD,
                                progress, progress_msg);
        
        sleep(1);
    }
    
    // 通知录像停止
    AI_GLASS_NOTIFY_STATE(task_id, AI_GLASS_MODULE_RECORD, CMD_RECORD,
                         AI_GLASS_STATE_STOPPING, "录像停止中");
    
    // 执行录像停止和清理
    ret = ak_record_stop();
    if (ret != 0) {
        AI_GLASS_NOTIFY_ERROR(task_id, AI_GLASS_MODULE_RECORD, CMD_RECORD,
                             AI_GLASS_ERR_RECORD_STOP_FAILED, "录像停止失败");
        return 0;
    }
    
    // 检查录像时长
    int actual_duration = ak_record_get_duration();
    if (actual_duration < record_min_sec) {
        AI_GLASS_NOTIFY_ERROR(task_id, AI_GLASS_MODULE_RECORD, CMD_RECORD,
                             AI_GLASS_ERR_RECORD_TIME_TOO_SHORT, "录像时间过短");
        return 0;
    }
    
    // 通知录像完成
    AI_GLASS_NOTIFY_SUCCESS(task_id, AI_GLASS_MODULE_RECORD, CMD_RECORD, 100, "录像完成");
    
    return task_id;
}

/* ==================== 使用示例 ==================== */

/**
 * @brief 使用示例函数
 */
void ai_glass_callback_usage_example(void)
{
    // 初始化回调系统
    if (ai_glass_init_callback_system() != 0) {
        ai_glass_log_error("回调系统初始化失败");
        return;
    }
    
    // 示例1: 执行拍照
    ai_glass_log_info("=== 拍照示例 ===");
    uint32_t photo_task_id = run_take_photo_with_callback(SNAP_ISP_CONFIG, 30, 4);
    if (photo_task_id > 0) {
        ai_glass_log_info("拍照任务启动，任务ID: %u", photo_task_id);
    }
    
    sleep(2);
    
    // 示例2: 执行录像
    ai_glass_log_info("=== 录像示例 ===");
    char *record_file_name = NULL;
    uint32_t record_task_id = run_record_with_callback(VIDEO_ISP_CONFIG, 0, 10, 3, 4, &record_file_name);
    if (record_task_id > 0) {
        ai_glass_log_info("录像任务启动，任务ID: %u", record_task_id);
    }
    
    sleep(5);
    
    // 示例3: 手动发送通知
    ai_glass_log_info("=== 手动通知示例 ===");
    uint32_t manual_task_id = ai_glass_callback_generate_task_id();
    AI_GLASS_NOTIFY_STATE(manual_task_id, AI_GLASS_MODULE_NETWORK, CMD_WIFI_STA,
                         AI_GLASS_STATE_RUNNING, "WiFi连接中");
    
    sleep(1);
    
    AI_GLASS_NOTIFY_SUCCESS(manual_task_id, AI_GLASS_MODULE_NETWORK, CMD_WIFI_STA,
                           100, "WiFi连接成功");
    
    // 清理回调系统
    ai_glass_callback_destroy();
}